package factory.one.controller;

import factory.one.base.Constant;
import factory.one.entity.Batch;
import factory.one.entity.Custom;
import factory.one.entity.Order;
import factory.one.entity.Product;
import factory.one.pojo.common.Page;
import factory.one.pojo.common.SqlCondition;
import factory.one.service.*;
import factory.one.utils.StageUtil;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.sql.Types;
import java.util.Collections;
import java.util.List;

/**
 * @author sk.z
 * 2023/7/28
 * 车间工作
 **/
@Controller
@RequestMapping("/work")
public class WorkController {

    private final OrderService orderService;
    private final BatchService batchService;
    private final ProductService productService;
    private final CustomService customService;
    private final CountService countService;

    public WorkController(OrderService orderService,
                          BatchService batchService,
                          ProductService productService,
                          CustomService customService,
                          CountService countService) {
        this.orderService = orderService;
        this.batchService = batchService;
        this.productService = productService;
        this.customService = customService;
        this.countService = countService;
    }

    @GetMapping("/list.html")
    public String listHtml(@RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "20") Integer size,
                           Model model) {
        SqlCondition condition = new SqlCondition("is_confirm = ?",true, Types.BOOLEAN); //已上产线
        List<Order> list = orderService.listByCondition(page, size, Collections.singletonList(condition));
        Integer count = orderService.countByCondition(Collections.singletonList(condition));
        trans(list);
        model.addAttribute("page", new Page<>(list, count, page, size));
        return "work/list";
    }

    @GetMapping("/product.html")
    public String productHtml(@RequestParam Long orderId, Model model) {
        List<Batch> batchList = batchService.list(orderId);
        transBatch(batchList);
        model.addAttribute("order", orderService.getById(orderId));
        model.addAttribute("batchList", batchList);
        return "work/product";
    }

    @ResponseBody
    @PostMapping("/product")
    public Integer product(@RequestParam Long id, @RequestParam Integer producedNumber){
        String userName = SecurityContextHolder.getContext().getAuthentication().getName();
        Long orderId = batchService.getById(id).getOrderId();
        countService.submit(orderId);
        return batchService.updateProduceIncrease(id,producedNumber,userName);
    }

    @GetMapping("/send.html")
    public String sendHtml(@RequestParam Long orderId, Model model) {
        Order order = orderService.getById(orderId);
        List<Batch> batchList = batchService.list(orderId);
        Custom custom = customService.getById(order.getCustomId());
        order.setCustomName(custom.getName());
        transBatch(batchList);
        model.addAttribute("order", order);
        model.addAttribute("batchList", batchList);
        model.addAttribute("corporate", Constant.CORPORATE_NAME);
        return "work/send";
    }

    @ResponseBody
    @PostMapping("/send")
    public Integer send(@RequestParam Long id){
//        return orderService.updateSend(id);
        return 0;
    }

    private void trans(List<Order> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        for (Order order : list) {
            Custom custom = customService.getById(order.getCustomId());
            order.setCustomName(custom.getName());
//            StageUtil.build(order);
        }
    }

    private void transBatch(List<Batch> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        for (Batch batch : list) {
            Product product = productService.getById(batch.getProductId());
            batch.setProductName(product.getName());
            batch.setAmount(batch.getSendNum() * batch.getPrice());
            batch.setProduceGap(Math.max(0, batch.getOrderNum() - batch.getProduceNum()));
            batch.setSendGap(Math.max(0, batch.getOrderNum() - batch.getSendNum()));
        }
    }

}
